import PrefsUtil from "./AshFramework/Utils/PrefsUtil";
import { ZTool } from "./AshFramework/Utils/ZTool";


export class PlayerData {
    constructor() { }

    //等级
    get LV() { return PrefsUtil.GetNumber(`Player_LV`, 1); }
    set LV(value: number) {
        value = ZTool.Clamp(value, 0, PlayerData.HPs.length - 1);
        PrefsUtil.SetNumber(`Player_LV`, value);
    }

    get HP() { return PlayerData.HPs[this.LV] }

    //经验
    get EXP() { return PrefsUtil.GetNumber(`Player_EXP`, 0) }
    set EXP(value: number) {
        //升级
        if (value >= PlayerData.EXPs[this.LV]) {
            this.LV++;
            value -= PlayerData.EXPs[this.LV];
        }
        PrefsUtil.SetNumber(`Player_EXP`, value);
    }

    //最大血量
    get MAXHP(): number { return PlayerData.HPs[this.LV] }

    static EXPs: number[] = [0, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000];
    static HPs: number[] = [0, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000];
}

export class ItemData {
    static MaxLv = 8;
    static MaxEquipItem = 6;
    static IsKnife(index: number) { return [2, 3, 5, 8, 9, 10].findIndex(e => e == index) != -1 }

    constructor(name: string, index: number, quality: number, price: number, upgratePieces: string, upgratePrice: string, damages: string, pushForce: string, damageRaius: string) {
        this.Name = name;
        this.Index = index;
        this.Quality = quality;
        this.Price = price;

        upgratePieces.split(",").forEach(e => this.UpgratePieces.push(Number(e)));
        upgratePrice.split(",").forEach(e => this.UpgratePrices.push(Number(e)));
        damages.split(",").forEach(e => this.Damages.push(Number(e)));
        pushForce.split(",").forEach(e => this.PushForces.push(Number(e)));
        damageRaius.split(",").forEach(e => this.DamageRadii.push(Number(e)));
    }

    Name: string = "";
    Index: number;
    Quality: number = 0;
    Selected: number = 0;
    Price: number = 0;

    private UpgratePieces: number[] = [];
    private UpgratePrices: number[] = [];
    Damages: number[] = [];
    private PushForces: number[] = [];
    private DamageRadii: number[] = [];

    //解锁
    get IsUnlock() {
        if (this.Price == 0) return true;
        return PrefsUtil.GetBool(`Item_${this.Index}_IsUnlock`);
    }
    set IsUnlock(value: boolean) {
        PrefsUtil.SetBool(`Item_${this.Index}_IsUnlock`, value);
    }

    get IsMaxLv() { return this.Lv >= ItemData.MaxLv; }

    //等级
    get Lv() { return PrefsUtil.GetNumber(`Item_${this.Index}_Lv`, 1) }
    set Lv(value: number) {
        value = ZTool.Clamp(value, 1, 10);
        PrefsUtil.SetNumber(`Item_${this.Index}_Lv`, value);
    }

    //碎片
    get Piece() { return PrefsUtil.GetNumber(`Item_${this.Index}_Piece`, 0) }
    set Piece(value: number) {
        PrefsUtil.SetNumber(`Item_${this.Index}_Piece`, value);
    }

    get UpgratePiece() { return this.UpgratePieces[this.Lv] }
    get UpgratePrice() { return this.UpgratePrices[this.Lv] }
    get Damage() { return this.Damages[this.Lv] }
    get PushForce() { return this.PushForces[this.Lv] }
    get DamageRaduis() { return this.DamageRadii[this.Lv] }
}

export class HeroData {
    constructor(index: number, quality: number, price: number) {
        this.Index = index;
        this.Quality = quality;
        this.Price = price;
    }

    Index: number;
    Quality: number = 0;
    Price: number = 0;

    //等级
    get LV() { return PrefsUtil.GetNumber(`Hero_${this.Index}_LV`, 1) }
    set LV(value: number) {
        PrefsUtil.SetNumber(`Hero_${this.Index}_LV`, value);
    }

    get IsSelected() { return PrefsUtil.GetBool(`Hero_${this.Index}_IsSelected`, false); }
    set IsSelected(value: boolean) { PrefsUtil.SetBool(`Hero_${this.Index}_IsSelected`, value); }

    //解锁
    get IsUnlock() {
        if (this.Price == 0) return true;
        return PrefsUtil.GetBool(`Hero_${this.Index}_IsUnlock`);
    }
    set IsUnlock(value: boolean) {
        PrefsUtil.SetBool(`Hero_${this.Index}_IsUnlock`, value);
    }
}